<!DOCTYPE html>
<!--
# @Time : 2021/9/22 16:12 
# @Author : 常雷
# @File : login.thml
# @Software: PyCharm
-->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
    <meta name="viewport" content="width-decice-width, initial-scale=1">
    {% load static %}
    <Link href="{% static 'css/bootstrap.min.css' %}" rel="styLesheet">
    <script src="{% static 'js/jquery.min.js' %}"></script>
    <script src="{% static 'js/bootstrap.min.js' %}"></script>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 col-lg-offset-2">
            <h1 class="text-center">登录</h1>
            <div class="form-group">
                <label for="username">用户昵称</label>
                <input type="text" name="username" id="username" class="form-control">
            </div>
            <div class="form-group">
                <label for="password">用户密码</label>
                <input type="password" name="password" id="password" class="form-control">
            </div>
            <div class="form-group">
                <label for="">验证码</label>
                <div class="row">
                    <div class="col-md-6">
                        <input type="text" name="code" id="id_code" class="form-control">
                    </div>
                    <div class="col-md-6">
                        <img src="/get_code/" alt="" width="350" height="34" id="id_img">
{#                        <img src="{% static 'img/default.png' %}" alt="" width="380" height="34">#}
                    </div>
                </div>
            </div>
            <input type="button" class="btn btn-success" value="登录" id="id_commit">
            <span style="color: red" id="id_error"></span>
        </div>
    </div>
</div>
<script>
    $("#id_img").click(function (){
        //1.先获取标签之前的src
        let oldval = $(this).attr('src');
        $(this).attr('src', oldval += '?' )
    })
    // 点击按钮发送ajax请求
    $("#id_commit").click(function (){
        $.ajax({
            url: '',
            type: 'post',
            data:{
                'username':$('#username').val(),
                'password':$('#password').val(),
                'code':$('#id_code').val(),
                'csrfmiddlewaretoken': '{{ csrf_token }}'
            },
            success:function (args){
                if(args.code == 1000){
                    // 跳转到首页
                    window.location.href = args.url
                }else {
                    // 渲染错误信息
                    $('#id_error').text(args.msg)
                }
            }
        })
    })
</script>
</body>
</html>